package engine.util.model;

import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;

import static org.lwjgl.opengl.GL11.*;

public class Material {
	private FloatBuffer ambient;
	private FloatBuffer diffuse;
	private FloatBuffer specular;
	private FloatBuffer emmision;
	private float shininess;
	
	public Material(float[] ambient, float[] diffuse, float[] specular, float[] emmision, float shininess) {
		if(ambient == null) {
			ambient = new float[] {0, 0, 0, 1};
		}
		
		if(diffuse == null) {
			 diffuse = new float[] {0, 0, 0, 1};
		}
		
		if(specular == null) {
			 specular = new float[] {0, 0, 0, 1};
		}
		
		if(emmision == null) {
			 emmision = new float[] {0, 0, 0, 1};
		}
		
		this.ambient = BufferUtils.createFloatBuffer(ambient.length).put(ambient);
		this.diffuse = BufferUtils.createFloatBuffer(diffuse.length).put(diffuse);
		this.specular = BufferUtils.createFloatBuffer(specular.length).put(specular);
		this.emmision = BufferUtils.createFloatBuffer(emmision.length).put(emmision);
		this.shininess = shininess;
	}
	
	public void bind() {
		ambient.rewind();
		diffuse.rewind();
		specular.rewind();
		emmision.rewind();
		
		glMaterial(GL_FRONT_AND_BACK, GL_AMBIENT, ambient);
		glMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse);
		glMaterial(GL_FRONT_AND_BACK, GL_SPECULAR, specular);
		glMaterial(GL_FRONT_AND_BACK, GL_EMISSION, emmision);
		glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess);
	}
}
